<?php
namespace Home\Service;

use Think\Service\BasicService;

class OrderService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($model) {
        return $this->db->save($model);
    }

    public function find($condition) {
        return $this->db->find($condition);
    }

    public function query($condition) {
        return $this->db->where($condition)->select();
    }

    public function queryFields($condition, $data, $limit) {
        return $this->db->field($data)->where($condition)->limit($limit['start'], $limit['end'])->select();
    }

    public function queryCount($condition) {
        return $this->db->where($condition)->count();
    }

    public function queryNoBindOrder($UNID, $PHONE) {
        $sql = "select UNID,OPENID,NAME,AIRPORT,PDATE,PTIME,MODELS,DESTINATION,TYPE,STATE,OTYPE from t_order
				where UNID like '%" . $UNID . "%' and PHONE='$PHONE' and openid is null ";
        return $this->db->query($sql);
    }

    public function Add_task_fin($save) {
        return $this->db->add($save);
    }

    public function queryOrderLimit($condition, $limit, $uid, $type) {
        $sql = "select o.*,u.NICKNAME,u1.NAME as DRIVER,u2.NAME as SERVICE,u1.PHONE as DPHONE,u2.PHONE AS CPHONE,u1.WXNUM as DWXNUM,u2.WXNUM AS CWXNUM,tf.DRIVER AS DID,tf.SERVICE AS CID from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_user as u1 on tf.DRIVER=u1.UNID
				left join t_user as u2 on tf.SERVICE=u2.UNID
				where o.UNID like '%" . $condition . "%' ";
        // and (o.PDATE >= CURDATE() or o.STATE <>6 )
        if ($type == 0) {
            $sql .= " and tf.DRIVER='$uid' ";
        } else {
            $sql .= " and tf.SERVICE='$uid' ";
        }
        $sql .= " and o.STATE IN(3,4,5,6,8) order by o.STATE,PDATE DESC,PTIME ";
        if ($limit) {
            $sql .= " limit {$limit['start']},{$limit['end']}";
        }
        return $this->db->query($sql);
    }

    public function queryOrderLimitCount($condition, $uid, $type) {
        $sql = "select count(o.UNID) as count from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				where o.UNID like '%" . $condition . "%' ";
        // and (o.PDATE >= CURDATE() or o.STATE <>6 )
        if ($type == 0) {
            $sql .= " and tf.DRIVER='$uid' ";
        } else {
            $sql .= " and tf.SERVICE='$uid' ";
        }
        $sql .= " and o.STATE IN(3,4,5,6,8)";
        $data = $this->db->query($sql);
        return $data[0][count];
    }

    public function queryAllocateLimit($condition, $limit, $team) {
        $sql = "select o.*,u.NICKNAME,u1.NAME as DRIVER,u2.NAME as SERVICE,u1.PHONE as DPHONE,u2.PHONE AS CPHONE,u1.WXNUM as DWXNUM,u2.WXNUM AS CWXNUM,tf.DRIVER AS DID,tf.SERVICE AS CID,u1.OPENID as DOPENID,u2.OPENID as COPENID from t_order as o
				left join t_user as u on o.OPENID=u.OPENID
				left join t_task_fin as tf on o.UNID=tf.OID
				left join t_user as u1 on tf.DRIVER=u1.UNID
				left join t_user as u2 on tf.SERVICE=u2.UNID
				left join t_task as t on o.UNID=t.OID
				where o.UNID like '%" . $condition . "%' and (tf.TEAM='$team' or t.`OWNER`='$team') group by o.UNID ";
        if ($limit) {
            $sql .= "limit {$limit['start']},{$limit['end']}";
        }
        return $this->db->query($sql);
    }

    //SQL语句查询
    public function querySQL($SQL) {
        return $this->db->query($SQL);
    }

    //查找历史订单
    public function queryHistoryOrder($where) {
        return $this->db->where($where)->order('PDATE asc , PTIME asc ')->select();
    }

    //拦截是否可以下单
    public function Ordering($TYPE , $OPENID){
        $SQL = "SELECT * FROM t_order WHERE OTYPE = $TYPE AND (STATE  = -1 OR STATE =0 ) AND OPENID = '$OPENID' ";
        return $this->db->query($SQL);
    }

    //日程天数
    public function ScheDay($OID){
        $sql="SELECT * from t_charter_info
                where OID='$OID'
                group by DATETIME
                order by SORT";
        return $this->db->query($sql);
    } 
}

?>